<!--
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-08-14 15:31:12
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-11-19 16:01:57
 * @FilePath: /frontend/mall-frontend/src/views/modules/admin/ware/waresku.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="mod-waresku">
    <data-table ref="dataTableRef" :criterias="table.criterias" name="waresku" :initLoad="true" url="/ware/waresku/select">
      <!-- ----------------------------------------- 查询条件 ----------------------------------------- -->
      <template v-slot:search="criterias">
        <dt-search-item label="仓库" prop="wareId">
          <wareinfo-select v-model="criterias.wareId" ref="wareinfoSelectRef"></wareinfo-select>
        </dt-search-item>
        <dt-search-item label="SKU信息" prop="skuId">
          <sku-select v-model="criterias.skuId" ref="skuSelectRef"></sku-select>
        </dt-search-item>
      </template>
      <!-- ------------------------------------------ 工具栏 ------------------------------------------ -->
      <template v-slot:toolbar>
        <el-button-group>
          <el-button v-if="admin.isAuth('ware:waresku:save')" @click="() => addDataEvent()" type="primary">
            新增
          </el-button>
          <el-button v-if="admin.isAuth('ware:waresku:save')" @click="() => updateDataEvent()" type="warning">
            修改
          </el-button>
          <el-button v-if="admin.isAuth('ware:waresku:delete')" @click="() => deleteDataEvent()" type="danger">
            删除
          </el-button>
        </el-button-group>
      </template>
      <!-- ---------------------------------------- 数据表格列 ---------------------------------------- -->
      <el-table-column fixed prop="skuId" align="center" label="SKUID" width="180" />
      <el-table-column fixed prop="skuName" show-overflow-tooltip align="left" label="SKU名称" width="280" />
      <el-table-column prop="stock" align="right" label="库存数" width="120" />
      <el-table-column prop="locked" align="center" label="锁定库存" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.locked === 1">已锁定</el-tag>
          <el-tag v-else type="info">未锁定</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="wareName" show-overflow-tooltip align="left" label="仓库名称" />
    </data-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" @loadTableData="loadTableData"></add-or-update>
  </div>
</template>
<script lang="ts" setup>
import admin from "@/utils/admin";
import { ref, nextTick, reactive } from "vue";

import SkuSelect from "../common/sku-select.vue";
import WareinfoSelect from "../common/wareinfo-select.vue";
import AddOrUpdate from "./waresku-add-or-update.vue";

// 数据表格
const table = reactive({
  criterias: {
    wareId: "",
    skuId: "",
  },
});

// 数据表格引用
const dataTableRef = ref();

// 弹窗引用
const addOrUpdateRef = ref();

// 新增修改弹窗是否显示
const addOrUpdateVisible = ref(false);

// 新增
const addDataEvent = async () => {
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("add");
};

// 修改
const updateDataEvent = async () => {
  // 验证并获取是否选择一条数据
  let data = admin.obtainOneSelectionTableData(dataTableRef.value);
  if (!data) {
    return;
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("update", data);
};

// 删除
const deleteDataEvent = () => {
  admin.batchDeleteSelectionTableData(dataTableRef.value, "/ware/waresku/delete");
};

// 表格数据加载
const loadTableData = () => {
  dataTableRef.value.loadTableData();
};
</script>
